package com.cqs.leetcode.math;

import java.util.*;

/**
 * lixw
 * 2021/11/16
 **/
public class PerfectRectangle391 {

    private class Point {
        int x;
        int y;


        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Point point = (Point) o;
            return x == point.x && y == point.y;
        }

        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }
    }

    private int rectangleArea = 0;

    public boolean isRectangleCover(int[][] rectangles) {
        Map<Point, Integer> map = new HashMap<>();
        for (int[] rectangle : rectangles) {
            cntPointFrequencyAndArea(map, rectangle);
        }
        List<Point> list = new ArrayList<>();
        for (Point point : map.keySet()) {
            int val = map.get(point);
            if (val == 1) {
                list.add(point);
            } else if (val != 2 && val != 4) {
                return false;
            }
        }
        //4个顶点
        if (list.size() != 4) return false;
        list.sort((o1, o2) -> {
            if (o1.x != o2.x) return o1.x - o2.x;
            return o1.y - o2.y;
        });
        Point p0 = list.get(0);
        Point p3 = list.get(3);
        if (p0.x != list.get(1).x || p0.y != list.get(2).y) return false;
        if (list.get(1).y != p3.y || list.get(2).x != p3.x) return false;
        //求四个顶点组成的面积
        int rectArea = area(new int[]{p0.x, p0.y, p3.x, p3.y});
        return rectArea == rectangleArea;
    }

    private void cntPointFrequencyAndArea(Map<Point, Integer> map, int[] rectangle) {
        Point p1 = new Point(rectangle[0], rectangle[1]);
        map.put(p1, map.getOrDefault(p1, 0) + 1);
        Point p2 = new Point(rectangle[2], rectangle[1]);
        map.put(p2, map.getOrDefault(p2, 0) + 1);
        Point p3 = new Point(rectangle[2], rectangle[3]);
        map.put(p3, map.getOrDefault(p3, 0) + 1);
        Point p4 = new Point(rectangle[0], rectangle[3]);
        map.put(p4, map.getOrDefault(p4, 0) + 1);
        rectangleArea += area(rectangle);
    }

    private int area(int[] rectangle) {
        return (rectangle[2] - rectangle[0]) * (rectangle[3] - rectangle[1]) ;
    }

    public static void main(String[] args) {
        List<int[][]> list = new ArrayList<>();
        list.add(new int[][]{{1, 1, 3, 3}, {3, 1, 4, 2}, {3, 2, 4, 4}, {1, 3, 2, 4}, {2, 3, 3, 4}});
        list.add(new int[][]{{1, 1, 2, 3}, {1, 3, 2, 4}, {3, 1, 4, 2}, {3, 2, 4, 4}});
        list.add(new int[][]{{1, 1, 3, 3}, {3, 1, 4, 2}, {1, 3, 2, 4}, {3, 2, 4, 4}});
        list.add(new int[][]{{1, 1, 3, 3}, {3, 1, 4, 2}, {1, 3, 2, 4}, {2, 2, 4, 4}});

        PerfectRectangle391 p = new PerfectRectangle391();
        for (int[][] rect : list) {
            System.out.println(p.isRectangleCover(rect));
        }

    }
}
